package cn.piesat.scanning.business.xz.service.forimpl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import cn.piesat.scanning.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildXIZANG_FOR_DIFFTServiceImpl implements BuildAlgorithmTaskService {

    @Autowired
    private HtScanningMetadataService htScanningMetadataService;
    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam, AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate, String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
        //森林历史同期算法
        Map<String, Object> params = new HashMap<>();
        //GSE_PLIJ_540000_L3_FY3D_MERSI_1000M_GLL_MOAD_20210701000000_20210701235959.TIFF
        params.put("primaryFile", primaryFile);
        String fileName = primaryFile.substring(primaryFile.lastIndexOf("/") + 1);
        String[] s1 = fileName.split("_");
        String moduleIid = s1[0];
        String level = s1[3];
        String satellite = s1[4];
        String product = s1[1];
        String productMode = s1[8];
        String region = s1[2];
        String dataTime = DateUtils.getDateStr(primaryFile, "(\\d{14})");
        Date date = DateUtil.parse(dataTime, "yyyyMMddHHmmss");
        Date endTime = DateUtil.offsetDay(date, -1);
        String endTimeStr = DateUtil.format(endTime, "yyyyMMddHHmmss");
        Date startTime = DateUtil.offsetMonth(date, -60);
        String dateTimeStr = DateUtil.format(startTime, "yyyyMMddHHmmss");
        List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findOneByDate(dateTimeStr, endTimeStr, level, satellite,
                product.replace("-", ""), region, productMode, moduleIid, "TIFF");
        List<String> collect = metadataDTOList.stream()
                .sorted(Comparator.comparing(HtScanningMetadataDTO::getFileDate))
                .map(x -> taskPathParam.getParameter02() + File.separator + x.getFilePath())
                .collect(Collectors.toList());
        String join = StrUtil.join(",", collect);
        if (metadataDTOList.size() == 0) {
            algorithmParams.put("parameter01", null);
        } else {
            params.put("primaryFile", primaryFile);
            params.put("lastFile", join);
            params.put("satelliteName", satellite);
            params.put("auxPath", taskPathParam.getParameter03());
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        }
        return algorithmParams;
    }
}
